package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.ManagerTeacher;
import com.markerhub.mapper.ManagerClassMapper;
import com.markerhub.mapper.ManagerTeacherMapper;
import com.markerhub.service.ManagerTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManagerTeacherServiceImpl extends ServiceImpl<ManagerTeacherMapper, ManagerTeacher> implements ManagerTeacherService {
@Autowired
    ManagerClassMapper classMapper;
    @Override
    public ManagerTeacher getByTeachername(String teachername) {
        // 使用 MyBatis-Plus 查询构造器，通过 teachername 字段查询
        return this.getOne(new LambdaQueryWrapper<ManagerTeacher>().eq(ManagerTeacher::getTeacherName, teachername));
    }
    @Override
    public IPage getTeachers(Integer id, String teacherName, int currentPage, int pageSize) {
        Page<ManagerTeacher> page = new Page<>(currentPage, pageSize);

        QueryWrapper<ManagerTeacher> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.eq("id", id); // 根据教师ID查询
        }
        if (teacherName != null && !teacherName.isEmpty()) {
            queryWrapper.like("teacher_name", teacherName); // 模糊查询教师姓名
        }
        queryWrapper.orderByDesc("id"); // 按照教师ID降序排序

        IPage page1 = this.page(page, queryWrapper);
        return page1;
    }

    @Override
    public boolean checkTeacherRelations(Long teacherId) {
        // 检查是否有关联的班级
        Integer classCount = classMapper.countByTeacherId(teacherId);
        return classCount > 0;
    }
}
